package cn.gok.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author shikundai
 * @date 2021/3/24
 * @time 11:17
 * @description: 测试控制器
 */
@RestController
@RequestMapping("/test")
@CrossOrigin(origins = {"http://127.0.0.1:8084"})
public class TestConreoller {
    /**
     * 描述：测试控制器是否可用
     * @author shikundai
     * @date 2021/3/24
     * @time 15:06
     */
    @GetMapping("/t1")
    public String test1(){
        return "测试OK";
    }
    /**
     * 描述：测试shiro权限注解
     * 一旦加上这个注解必须要有这个角色才能访问
     * @author shikundai
     * @date 2021/3/24
     * @time 14:53
     */
    @GetMapping("/t2")
    @RequiresRoles("teacher")
    public String test2() {
        return "访问到了test2";
    }
    @GetMapping("/t3")
    public Map test3(){
        Map map=new HashMap();
        map.put("name","代时坤");
        map.put("psw","12345");
        return map;
    }

    @GetMapping("/login")
    @CrossOrigin(origins = {"http://127.0.0.1:5500"})
    public String testLogin(){
        UsernamePasswordToken token=new UsernamePasswordToken("dsk","dsk");
        SecurityUtils.getSubject().login(token);
        System.out.println(SecurityUtils.getSubject().getPrincipals());
        System.out.println(SecurityUtils.getSubject().getSession().getId());
        return "OK";
    }
    @GetMapping("/getUser")
    @CrossOrigin(origins = {"http://127.0.0.1:5500"})
    public String getLogin(){
        Subject subject=SecurityUtils.getSubject();
        Object obj=subject.getPrincipals();
        System.out.println(obj);
        System.out.println(SecurityUtils.getSubject().isAuthenticated());

        return "OK";
    }

    @GetMapping("/logOut")
    @CrossOrigin(origins = {"http://127.0.0.1:5500"})
    public String logOut(){
        Subject subject=SecurityUtils.getSubject();
        Object obj=subject.getPrincipals();
        System.out.println(obj);
        System.out.println(SecurityUtils.getSubject().isAuthenticated());
        SecurityUtils.getSubject().logout();
        return "OK";
    }

}
